package com.sihai.ehr.service.impl;

import java.text.MessageFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.management.RuntimeErrorException;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.EmployeeWorkExperience;
import com.sihai.ehr.model.biz.EmployeeinfoBase;
import com.sihai.ehr.model.biz.EmployeeinfoEducation;
import com.sihai.ehr.model.biz.EmployeeinfoFamily;
import com.sihai.ehr.model.biz.EmployeeinfoTrain;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.service.EmployeeinfoService;

/**
 * 员工信息服务实现类
 * 
 * @author henry
 * 
 */
@Service("employeeinfoService")
public class EmployeeinfoServiceImpl implements EmployeeinfoService {

	@Resource
	private DataAccessor dataAccessor;

	@Override
	public EmployeeinfoBase getEmployeeinfoBase(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeErrorException(new Error("【传入参数非法】"));
		}
		try {
			return dataAccessor.get(EmployeeinfoBase.class, id);
		} catch (Exception e) {
			throw new RuntimeErrorException(
					new Error(
							"【异常原因：读取员工基本资料信息数据库发生异常】【发生地点：EmployeeinfoServiceImpl.getEmployeeinfoBase;参数id="
									+ id + " 】"));
		}

	}

	@Override
	public List<EmployeeinfoBase> searchEmployeebaseinfos(String name,
			String state, Limit limit, Sorter sorter) {
		if (state == null || state.isEmpty()) {
			throw new RuntimeErrorException(new Error("【state参数异常】"));
		}
		try {
			if (name == null || name.isEmpty()) {
				return dataAccessor.findByHql(EmployeeinfoBase.class,
						"from EmployeeinfoBase where state=?",
						new Object[] { state }, limit, sorter);
			} else {
				return dataAccessor.findByHql(EmployeeinfoBase.class,
						"from EmployeeinfoBase where state=? and name=?",
						new Object[] { state, name }, limit, sorter);
			}
		} catch (Exception e) {
			throw new RuntimeErrorException(
					new Error(
							"【异常原因：查询限制条件下的员工基本信息数据库发生异常】【发生地点：EmployeeinfoServiceImpl.searchEmployeebaseinfos;参数name="
									+ name + " 】"));
		}
	}

	@Override
	public LimitedList<EmployeeinfoBase> searchEmployeebaseinfos(
			final EmployeeinfoBase employeeinfoBase, Limit limit, Sorter sorter) {
		if (employeeinfoBase == null) {
			try {
				return dataAccessor.findByClass(EmployeeinfoBase.class, limit,
						sorter);
			} catch (Exception e) {
				throw new RuntimeErrorException(
						new Error(
								"【异常原因：查询限制条件下的员工基本信息数据库发生异常】【发生地点：EmployeeinfoServiceImpl.searchEmployeebaseinfos;参数employeeinfoBase为空】"));
			}
		}
		return dataAccessor.findByQuery(EmployeeinfoBase.class,
				new QueryCreator() {

					@Override
					public Query createQuery(Session session) {
						DynamicQuery dynamicQuery = new DynamicQuery(
								"from EmployeeinfoBase e where 1=1");
						if (employeeinfoBase.getState() != null
								&& !employeeinfoBase.getState().isEmpty()) {
							dynamicQuery.appendIfNotNull(
									"and e.state = :state", "state",
									employeeinfoBase.getState());
						}
						dynamicQuery.appendIfNotNull("and e.name like :name",
								"name", employeeinfoBase.getName());
//						dynamicQuery.appendIfNotNull(
//								"and d.employeeNo like :employeeNo",
//								"employeeNo", employeeinfoBase.getEmployeeNo());

						return dynamicQuery.build(session);
					}
				}, limit, sorter);
	}

	@Override
	public void saveEmployeeinfoBase(EmployeeinfoBase employeeinfoBase) {
		if (employeeinfoBase == null) {
			throw new RuntimeException("【employeeinfobase对象参数异常】");
		}
		try {
			dataAccessor.save(employeeinfoBase);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保存员工基本资料信息数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：EmployeeinfoServiceImpl.saveEmployeeinfoBase】");
		}
	}

	@Override
	public void removeEmployeeinfoBase(final String employeebaseinfoIds) {
		if (employeebaseinfoIds == null || employeebaseinfoIds.isEmpty()) {
			throw new RuntimeException("【参数异常：employeebaseinfoIds为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {

				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("update EmployeeinfoBase e set d.state=2 where id in {0}",
									employeebaseinfoIds));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：移除员工基本信息数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：EmployeeinfoServiceImpl.removeEmployeeinfoBase】");
		}

	}

	@Override
	public EmployeeinfoEducation getEmployeeinfoEducation(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法教育单元id为空】");
		}
		try {
			return dataAccessor.get(EmployeeinfoEducation.class, id);
		} catch (Exception e) {
			throw new RuntimeErrorException(
					new Error(
							"【异常原因：获取员工教育单元情况数据库发生异常】【异常说明："
									+ e.getMessage()
									+ "】【发生地点：EmployeeinfoServiceImpl.getEmployeeinfoBase;参数id="
									+ id + " 】"));
		}
	}

	@Override
	public List<EmployeeinfoEducation> searchEmployeeinfoEducations(
			Long employeeInfoId, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public LimitedList<EmployeeinfoEducation> searchEmployeeinfoEducations(
			EmployeeinfoEducation employeeinfoEducation, Limit limit,
			Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveEmployeeInfoEducation(
			EmployeeinfoEducation employeeinfoEducation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeInfoEducationByIds(String employeeEducationIds) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeInfoEducationByEmployeeinfobaseId(
			Long employeeInfoId) {
		// TODO Auto-generated method stub

	}

	@Override
	public EmployeeinfoTrain getEmployeeinfoTrain(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EmployeeinfoTrain> searchEmployeeinfoTrains(
			Long employeeInfoId, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public LimitedList<EmployeeinfoTrain> searchEmployeeinfoTrains(
			EmployeeinfoTrain employeeinfoTrain, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveEmployeeinfoTrain(EmployeeinfoTrain employeeinfoTrain) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeinfoTrainByIds(String employeeinfotrainIds) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeinfoTrainByEmployeeinfobaseId(
			Long employeeInfoId, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub

	}

	@Override
	public EmployeeWorkExperience getEmployeeWorkExperience(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EmployeeWorkExperience> searchEmployeeWorkExperiences(
			Long employeeInfoId, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public LimitedList<EmployeeWorkExperience> searchEmployeeWorkExperiences(
			EmployeeWorkExperience employeeWorkExperience, Limit limit,
			Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveEmployeeWorkExperience(
			EmployeeWorkExperience employeeWorkExperience) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeWorkExperienceByIds(
			String employeeWorkExperienceIds) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeWorkExperienceByEmployeeinfobaseId(
			Long employeeInfoId) {
		// TODO Auto-generated method stub

	}

	@Override
	public EmployeeinfoFamily getEmployeeinfoFamily(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EmployeeinfoFamily> searchEmployeeinfoFamilies(
			Long employeeInfoId, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public LimitedList<EmployeeinfoFamily> searchEmployeeinfoFamilies(
			EmployeeinfoFamily employeeinfoFamily, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveEmployeeinfoFamily(EmployeeinfoFamily employeeinfoFamily) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeinfoFamilyByIds(String employeeinfoFamilyIds) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeinfoFamilyByEmployeeinfobaseId(Long employeeInfoId) {
		// TODO Auto-generated method stub

	}

	@Override
	public EmployeeinfoWork getEmployeeinfoWork(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EmployeeinfoWork> searchEmployeeinfoWorks(Long employeeInfoId,
			Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public LimitedList<EmployeeinfoWork> searchEmployeeinfoWorks(
			EmployeeinfoWork employeeinfoWork, Limit limit, Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void saveEmployeeinfoWork(EmployeeinfoWork employeeinfoWork) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeinfoWorkByIds(String employeeinfoWorkIds) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeEmployeeinfoWorkByEmployeeinfobaseId(Long employeeInfoId) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Object> queryEmployeeUnionInfo(String sql, Limit limit,
			Sorter sorter) {
		// TODO Auto-generated method stub
		return null;
	}
}
